<?php

/**
 * @author chenshubo
 * 总SASS关务对接接口（综合关务：岚和、麦帮、龙南）
 */
namespace App\Service\OrderService\OrderRouteService;
use App\Service\Code\Normal;
use Service\AppService\IdService;
use App\Model\PssModel\OrderSupplier;
use App\Service\HttpServer\HttpCurl;
use App\Service\VendorClient\ShopGwApi\ShopGwApiClient;
use App\Service\VendorClient\ShopGwApi\ShopPddUserGwApiClient;
use App\Service\OrderService\BaseSupplierRouterService;

class Shopcustom extends BaseSupplierRouterService
{
    /**
     * 创建订单
     * @param array $param
     * @return mixed
     */
    public function createOrder( $param = [] )
    {
        //生成供应商主单号
        $supplierGoodsInfo      = $param['supplierGoodsInfo'];
        $supplierCode           = $supplierGoodsInfo['supplierCode'];
        $apiBody                = $supplierGoodsInfo['apiBody'];
        $thirdPartyPlatformCode = $supplierGoodsInfo['thirdPartyPlatformCode'];
        //下单商品组
        $userGoodsList          = $supplierGoodsInfo['userGoodsList'];
        //下单商品
        $orderGoods = [];
        foreach ($userGoodsList as $item ) {
            $orderGoods = array_merge($orderGoods,$item);
            unset($item);
        }
        
        //下游是否申报
        $isDelare = (
            (isset($param['order']['isOrderDeclare']) && $param['order']['isOrderDeclare'] == 1) ||
            (isset($param['order']['isPayDeclare']) && $param['order']['isPayDeclare'] == 1)
            ) ? true : false;
        
        //是否云仓商城
        $isShop = isset($param['order']['isSplitOrder']) && $param['order']['isSplitOrder'] == 1 ? true : false ;
        
        //防止订单号重复问题,下游已申报，则使用下游的订单号作为中台子单号
        if( $isDelare && !$isShop ) {
            $param['order']['supplier_dsn'] = (string)$param['order']['orderSn'];
        } else {
            $param['order']['supplier_dsn'] = (string)(IdService::instance()->getOtherID());
        }
        $param['order']['supplier_psn'] = OrderSupplier::instance()->getCreateOrderSn($supplierCode);
        $param['order']['goods'] = $orderGoods;
        
        //调用上游创建订单接口
        $binfo = $param['binfo'];
        if( $binfo['user_tag'] == self::PDD_USER_TAG ) {//拼多多专用
            $res = $this->callSdkApi(ShopPddUserGwApiClient::class,$apiBody)->createOrder($param);
        } else {
            $res = $this->callSdkApi(ShopGwApiClient::class,$apiBody)->createOrder($param);
        }
        
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        
        $parameter = [
            [
                'tag'       => $thirdPartyPlatformCode ? $thirdPartyPlatformCode : $supplierCode,
                'uid'       => $param['order']['supplier_dsn'],
                'url'       => $res['data']['url'],
                'data'      => json_encode($res['data']['data'],JSON_UNESCAPED_UNICODE),
                'method'    => 'post',
                'param'     => json_encode($param,JSON_UNESCAPED_UNICODE),
                'format'    => $res['data']['format'],
                'header'    => [],
            ]
        ];
        return Normal::returnCode(Normal::SUC,"获取订单参数成功",[
            'orderPush' => $parameter
        ]);
    }

    /**
     * 订单支付
     * @param array $param
     * @return mixed
     */
    public function orderPay( $param = [] )
    {
        $binfo = $param['binfo'];
        if( $binfo['user_tag'] == self::PDD_USER_TAG ) {//拼多多专用
            return $this->callSdkApi(ShopPddUserGwApiClient::class,$param['apiBody'])->payOrder($param);
        } else {
            return $this->callSdkApi(ShopGwApiClient::class,$param['apiBody'])->payOrder($param);
        }
    }
    
    /**
     * 订单取消
     * @param array $param
     * @return mixed
     */
    public function orderCancel( $param = [] )
    {
        $binfo = $param['binfo'];
        if( $binfo['user_tag'] == self::PDD_USER_TAG ) {//拼多多专用
            return $this->callSdkApi(ShopPddUserGwApiClient::class,$param['apiBody'])->cancelOrder($param);
        } else {
            return $this->callSdkApi(ShopGwApiClient::class,$param['apiBody'])->cancelOrder($param);
        }
    }
    
    /**
     * 订单售后
     * @param array $param
     * @return mixed
     */
    public function orderAfterSale($param = [])
    {
        $binfo = $param['binfo'];
        if( $binfo['user_tag'] == self::PDD_USER_TAG ) {//拼多多专用
            return $this->callSdkApi(ShopPddUserGwApiClient::class,$param['apiBody'])->cancelOrder($param);
        } else {
            return $this->callSdkApi(ShopGwApiClient::class,$param['apiBody'])->cancelOrder($param);
        }
    }
    
    /**
     * 获取订单清关申报的报文
     * @return mixed
     */
    public function getOrderClearanceApplyConents( $param = [] )
    {
        $orderList  = $param['orderList'];
        $orderGoods = $param['orderGoods'];
        $supplier   = $param['supplier'];
        $contents = [];
        foreach ($orderList as $orderItem )
        {
            $res = $this->callSdkApi(ShopPddUserGwApiClient::class,$param['apiBody'])->queryOrderDeclare([
                'supplierOrder' => $orderItem,
                'orderGoods'    => $orderGoods[$orderItem['order_id']],
                'supplier'      => $supplier
            ]);
            if( $res['code'] != Normal::SUC ) {
                return $res;
            }
            $contents[] = $res['data'];
        }
        return Normal::returnCode(Normal::SUC,"获取当前供应商订单申报报文成功",$contents);
    }

    /**
     * 获取订单申报报文
     * @param array $param
     */
    public function getOrderDeclareContents( $param = [] )
    {
        $orderList  = $param['orderList'];
        $supplier   = $param['supplier'];
        $contents = [];
        $res = $this->callSdkApi(ShopPddUserGwApiClient::class,$supplier['apiBody'])->queryDeclareOrderContentsByOrderSns([
            'supplierOrderList' => $orderList,
            'supplier'          => $supplier
        ],'queryOrderDeclareContent');
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        $contents[] = $res['data'];
        return Normal::returnCode(Normal::SUC,"获取当前供应商订单申报报文成功",$contents);
    }

    /**
     * 获取运单申报报文
     * @param array $param
     */
    public function getLogisticDeclareContents( $param = [] )
    {
        $orderList  = $param['orderList'];
        $supplier   = $param['supplier'];
        $contents = [];
        $res = $this->callSdkApi(ShopPddUserGwApiClient::class,$supplier['apiBody'])->queryDeclareOrderContentsByOrderSns([
            'supplierOrderList' => $orderList,
            'supplier'          => $supplier
        ],'queryLogisticDeclare');
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        $contents[] = $res['data'];
        return Normal::returnCode(Normal::SUC,"获取当前供应商运单申报报文成功",$contents);
    }

    /**
     * 获取清单申报报文
     * @param array $param
     */
    public function getInventoryDeclareContents( $param = [])
    {
        $orderList  = $param['orderList'];
        $supplier   = $param['supplier'];
        $contents = [];
        $res = $this->callSdkApi(ShopPddUserGwApiClient::class,$supplier['apiBody'])->queryDeclareOrderContentsByOrderSns([
            'supplierOrderList' => $orderList,
            'supplier'          => $supplier
        ],'queryInventoryDeclare');
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        $contents[] = $res['data'];
        return Normal::returnCode(Normal::SUC,"获取当前供应商清单申报报文成功",$contents);
        
    }

    /**
     * 获取清单申报报文
     * @param array $param
     */
    public function getInventoryCancelContents( $param = [])
    {
        $orderList  = $param['orderList'];
        $supplier   = $param['supplier'];
        $contents = [];
        $res = $this->callSdkApi(ShopPddUserGwApiClient::class,$supplier['apiBody'])->queryDeclareOrderContentsByOrderSns([
            'supplierOrderList' => $orderList,
            'supplier'          => $supplier
        ],'queryInventoryCancel');
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        $contents[] = $res['data'];
        return Normal::returnCode(Normal::SUC,"获取当前供应商清单撤单申报报文成功",$contents);

    }
    

    /**
     * 申报结果通知
     * @param array $param
     */
    public function ReceiveDeclareResult( $param = [] )
    {
        $orderList  = $param['orderList'];
        $supplier   = $param['supplier'];
        $contents = [];
        $res = $this->callSdkApi(ShopPddUserGwApiClient::class,$supplier['apiBody'])->ReceiveDeclareResult([
            'resultOrderList'   => $orderList,
            'supplier'          => $supplier
        ]);
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        $contents[] = $res['data'];
        return Normal::returnCode(Normal::SUC,"当前供应商订单申报结果通知成功",$contents);
    }
    
    /**
     * 云仓通知关务进行撤单
     * @param array $param
     */
    public function OrderRevoke( $param = [] )
    {
        $supplierOrder = $param['supplierOrder'];
        $supplier = $param['companyInfo'];
        return $this->callSdkApi(ShopGwApiClient::class,$supplier['apiBody'])->OrderRevoke([
            'supplierOrder'     => $supplierOrder,
            'supplier'          => $supplier
        ]);
    }
    
    /**
     * 商品列表查询接口
     * @param array $param
     */
    public function queryGoodsList( $param = [] )
    {
        $res = $this->callSdkApi(ShopGwApiClient::class,$param['apiBody'])->{__FUNCTION__}($param['queryBody']);
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        return Normal::returnCode(Normal::SUC,"获取成功",[
            'total' => $res['data']['TotalCount'],
            'list'  => $res['data']['Datas']
        ]);
    }

    /**
     * 获取每个供应商接口响应报文
     * @param array $response
     */
    public function getResponseCode( $response = [],$request = [] )
    {
        $res = HttpCurl::instance()->getMicorServiceResponseInfo($response);
        if( $res['code'] == Normal::SUC ) {
            return Normal::returnCode(Normal::SUC,"操作成功",[
                'orderDeclareContents'  => $res['data']
            ]);
        }
        return Normal::returnCode(Normal::FAIL,$res['msg'],[
            'orderDeclareContents' => ''
        ]);
    }

    /**
     * 获取每个供应商接口响应报文
     * @param array $response
     */
    public function getResultCode( $response = [],$request = [] )
    {
        $res = HttpCurl::instance()->getMicorServiceResponseInfo($response);
        if( $res['code'] == Normal::SUC ) {
            return Normal::returnCode(Normal::SUC,"操作成功",$res['data']);
        }
        return Normal::returnCode(Normal::FAIL,$res['msg'],$res['data']);
    }
    
    /**
     * 获取响应code
     * @param array $param
     */
    public function getCreateOrderCode( $param = [] )
    {
        $res = HttpCurl::instance()->getMicorServiceResponseInfo($param);
        if( $res['code'] == Normal::SUC ) {
            return Normal::returnCode($res['code'],"OK",[
                'supplier_csn' => $res['data']['orderCode']
            ]);
        }
        return $res;
    }
}